package dao;

import Utils.C3P0Utils;
import Utils.PageBean;
import domain.Product;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by guo on 2017/10/23.
 */
public class ProductImpl implements ProductDao {
    @Override
    public List<Product> findAll() {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDs());
        String sql="select * from product";
        List list=null;
        try {
            list=(List) runner.query(sql,new BeanListHandler(Product.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Boolean Add(String pname, Double market_price, Double shop_price, String pdesc) throws SQLException {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDs());
        String sql="insert into product(pname,market_price,shop_price,pdesc)values(?,?,?,?)";
        int num=runner.update(sql,pname,market_price,shop_price,pdesc);
        if(num>0)
        {
            return true;
        }
        else
            return false;
    }

    @Override
    public Boolean Delete(int pid) throws SQLException {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDs());
        String sql="delete  from product where pid=?";
        int num=runner.update(sql,pid);
        if(num>0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    @Override
    public Product findRyId(int pid) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDs());
         String sql="select * from product where pid=?";
         Product product=null;
        try {
            product= (Product) runner.query(sql,new BeanHandler(Product.class),pid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return product;
    }

    @Override
    public List<Product> Search(String con) {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDs());
        String sql="select * from product where pname like ?";
        String str="%"+con+"%";
        List<Product> products=null;
        try {
            products= (List<Product>) runner.query(sql,new BeanListHandler(Product.class),str);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return products;
    }

    @Override
    public Boolean update(Product product) throws SQLException {
        QueryRunner runner=new QueryRunner(C3P0Utils.getDs());
        String sql="update product pname = ? ,market_price = ?,shop_price = ?,pdesc=? where pid = ?";
            int num= runner.update(sql,product.getPname(),product.getMarket_price(),product.getShop_price(),product.getPdesc(),product.getPid());
            if(num>0)
            {
                return true;
            }
         else {
                return false;
            }
    }

    @Override
    public PageBean<Product> Page(int pagesize, int currenpage) {
        PageBean<Product> pb=new PageBean();
        pb.setPageSize(pagesize);
        pb.setCurrenPage(currenpage);
        QueryRunner runner=new QueryRunner(C3P0Utils.getDs());
        String sql="select * from product order by pid limit ?,?";
        String sqlCount="select count(*) from product";
        try {
           List<Product>list= (List<Product>) runner.query(sql,new BeanListHandler(Product.class),((currenpage-1)*10),pagesize);
           pb.setPageList(list);
         Long c= (Long) runner.query(sqlCount,new ScalarHandler());
         int count=c.intValue();
         pb.setCount(count);
            System.out.println(count);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pb;
    }
}
